package com.example.apngdemo;

import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.vectordrawable.graphics.drawable.Animatable2Compat;

import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.bumptech.glide.Glide;
import com.github.penfeizhou.animation.apng.APNGDrawable;
import com.github.penfeizhou.animation.loader.AssetStreamLoader;

import org.qiyi.basecore.taskmanager.TM;
import org.qiyi.basecore.taskmanager.Task;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private RelativeLayout layoutRoot;
    private List<Task> taskList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        layoutRoot = findViewById(R.id.layoutRoot);
        taskList = new ArrayList<>();
        this.start();
    }

    /**
     * 添加任务至列表管理 便于统一cancel
     */
    private void addTaskToList() {
        for (Task task : taskList)
            task.cancel();
        taskList.clear();
        layoutRoot.removeAllViews();
        for (int i = 0; i < 13; i++) {
            final int index = i;
            taskList.add(new Task() {
                @Override
                public void doTask() {
                    switch (index) {
                        case 0:
                            doTask0();
                            break;
                        case 1:
                            doTask1();
                            break;
                        case 2:
                            doTask2();
                            break;
                        case 3:
                            doTask3();
                            break;
                        case 4:
                            doTask4();
                            break;
                        case 5:
                            doTask5();
                            break;
                        case 6:
                            doTask6();
                            break;
                        case 7:
                            doTask7();
                            break;
                        case 8:
                            doTask8();
                            break;
                        case 9:
                            doTask9();
                            break;
                        case 10:
                            doTask10();
                            break;
                        case 11:
                            doTask11();
                            break;
                        case 12:
                            doTask12();
                            break;
                    }
                }
            });
        }
    }

    public ImageView getIv(int width, int height) {
        ImageView iv = getIv();
        iv.setAdjustViewBounds(true);
        iv.setLayoutParams(getParams(width, height));
        return iv;
    }

    /**
     * 获取LayoutParams
     *
     * @param width  -1:MATCH_PARENT  -2:WRAP_CONTENT width+px
     * @param height -1:MATCH_PARENT  -2:WRAP_CONTENT height+px
     * @return
     */
    public RelativeLayout.LayoutParams getParams(int width, int height) {
        int widthSize, heightSize;
        if (width == -1) {
            widthSize = RelativeLayout.LayoutParams.MATCH_PARENT;
        } else if (width == -2) {
            widthSize = RelativeLayout.LayoutParams.WRAP_CONTENT;
        } else {
            widthSize = ConvertUtils.dp2px(width);
        }
        if (height == -1) {
            heightSize = RelativeLayout.LayoutParams.MATCH_PARENT;
        } else if (height == -2) {
            heightSize = RelativeLayout.LayoutParams.WRAP_CONTENT;
        } else {
            heightSize = ConvertUtils.dp2px(width);
        }
        return new RelativeLayout.LayoutParams(widthSize, heightSize);
    }

    /**
     * 获取ImageView组件
     *
     * @return
     */
    public ImageView getIv() {
        ImageView iv = new ImageView(this);
        iv.setAdjustViewBounds(true);
        return iv;
    }

    public APNGDrawable getApngDrawable(String name) {
        // 从asset file中加载
        AssetStreamLoader assetLoader = new AssetStreamLoader(this, name);
        // 创建APNG Drawable
        APNGDrawable apngDrawable = new APNGDrawable(assetLoader);
        return apngDrawable;
    }

    /**
     * 执行ListTask中的某个任务
     *
     * @param index
     */
    private void doTaskByIndex(int index) {
        this.doTaskByIndex(index, 0);
    }

    /**
     * 执行ListTask中的某个任务 带延迟执行
     *
     * @param index
     * @param delay
     */
    private void doTaskByIndex(int index, int delay) {
        if (taskList == null || taskList.size() - 1 < index)
            return;
        if (delay > 0)
            taskList.get(index).postUIDelay(delay);
        else
            taskList.get(index).executeSyncUI();
    }

    /**
     * 重新开始
     */
    private void restart() {
        this.addTaskToList();
        doTaskByIndex(0);
    }

    /**
     * 开启霸屏
     */
    public void start() {
        this.addTaskToList();
        doTaskByIndex(0);
        //定时任务 10分钟后关闭任务并移除所有子视图
        TM.postUIDelay(() -> {
            for (Task task : taskList)
                task.cancel();
            taskList.clear();
            layoutRoot.removeAllViews();
        }, 600000);
    }

    /**
     * 重金闪烁文字
     */
    private void doTask0() {
        ImageView iv = getIv(-1, -2);
        layoutRoot.addView(iv);
        APNGDrawable apngDrawable = getApngDrawable("重金_02_iSpt.png");
        apngDrawable.setLoopLimit(1);
        apngDrawable.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
            @Override
            public void onAnimationEnd(Drawable drawable) {
                super.onAnimationEnd(drawable);
                layoutRoot.removeView(iv);
            }
        });
        GlideHelper.loadApng(apngDrawable, iv);
        this.doTaskByIndex(1, 1300);
    }

    /**
     * 霸屏闪烁文字
     */
    private void doTask1() {
        ImageView iv = getIv(-1, -2);
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) iv.getLayoutParams();
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        iv.setLayoutParams(params);
        layoutRoot.addView(iv);
        APNGDrawable apngDrawable = getApngDrawable("霸屏_02_iSpt.png");
        apngDrawable.setLoopLimit(1);
        apngDrawable.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
            @Override
            public void onAnimationEnd(Drawable drawable) {
                super.onAnimationEnd(drawable);
                layoutRoot.removeView(iv);
            }
        });
        GlideHelper.loadApng(apngDrawable, iv);
        this.doTaskByIndex(2, 1500);
    }

    /**
     * 重金霸屏之后的转场
     */
    private void doTask2() {
        ImageView iv = getIv(-1, -1);
        layoutRoot.addView(iv);
        APNGDrawable apngDrawable = getApngDrawable("转场效果_00081_iSpt.png");
        apngDrawable.setLoopLimit(1);
        apngDrawable.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
            @Override
            public void onAnimationEnd(Drawable drawable) {
                super.onAnimationEnd(drawable);
                layoutRoot.removeView(iv);
                MainActivity.this.doTaskByIndex(3);
                MainActivity.this.doTaskByIndex(4, 800);
                MainActivity.this.doTaskByIndex(5);
                MainActivity.this.doTaskByIndex(6, 3000);
            }
        });
        GlideHelper.loadApng(apngDrawable, iv);
    }

    /**
     * SUPERSHOW闪光文字
     */
    private void doTask3() {
        ImageView iv = getIv(-2, -2);
        iv.setAdjustViewBounds(true);
        layoutRoot.addView(iv);
        APNGDrawable apngDrawable = getApngDrawable("SUPERSHOW闪光.png");
        apngDrawable.setLoopLimit(1);
        GlideHelper.loadApng(apngDrawable, iv);
    }

    /**
     * 美元链条
     */
    private void doTask4() {
        ImageView iv = getIv(-1, -2);
        iv.setAdjustViewBounds(false);
        layoutRoot.addView(iv);
        APNGDrawable apngDrawable = getApngDrawable("美元链条_00031_iSpt.png");
        apngDrawable.setLoopLimit(1);
        GlideHelper.loadApng(apngDrawable, iv);
    }

    /**
     * 头像皇冠+名字边框+缩放动画
     */
    private void doTask5() {
        // 根布局
        RelativeLayout relativeLayout = new RelativeLayout(this);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ConvertUtils.dp2px(610), ConvertUtils.dp2px(300));
        layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        layoutParams.bottomMargin = ConvertUtils.dp2px(100);
        relativeLayout.setLayoutParams(layoutParams);
        layoutRoot.addView(relativeLayout);

        //名字边框
        ImageView nameFrameIv = getIv(310, 100);
        RelativeLayout.LayoutParams nameFrameParams = (RelativeLayout.LayoutParams) nameFrameIv.getLayoutParams();
        nameFrameParams.addRule(RelativeLayout.ALIGN_PARENT_END);
        nameFrameParams.topMargin = ConvertUtils.dp2px(80);
        nameFrameIv.setLayoutParams(nameFrameParams);
        relativeLayout.addView(nameFrameIv);
        APNGDrawable apngDrawable = getApngDrawable("名字框_00000_iSpt.png");
        apngDrawable.setLoopLimit(1);
        GlideHelper.loadApng(apngDrawable, nameFrameIv);

        //皇冠
        ImageView crownIv = getIv(300, 300);
        RelativeLayout.LayoutParams crownParams = (RelativeLayout.LayoutParams) crownIv.getLayoutParams();
        crownIv.setLayoutParams(crownParams);
        relativeLayout.addView(crownIv);
        APNGDrawable apngDrawableCrown = getApngDrawable("头像框_00000_iSpt.png");
        apngDrawable.setLoopLimit(1);
        GlideHelper.loadApng(apngDrawableCrown, crownIv);

        //加载圆角头像
        ImageView avatarIv = getIv(180, 180);
        RelativeLayout.LayoutParams avatarParams = (RelativeLayout.LayoutParams) avatarIv.getLayoutParams();
        avatarParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        avatarParams.leftMargin = ConvertUtils.dp2px(66);
        avatarParams.bottomMargin = ConvertUtils.dp2px(24);
        Glide.with(this).load("http://www.520touxiang.com/uploads/allimg/2018030608/qqpkuua1o1z.jpg").circleCrop().into(avatarIv);
        relativeLayout.addView(avatarIv);

        //加载姓名
        TextView nameTv = new TextView(this);
        RelativeLayout.LayoutParams nameTvParams = getParams(280, -2);
        nameTvParams.topMargin = ConvertUtils.dp2px(172);
        nameTvParams.leftMargin = ConvertUtils.dp2px(280);
        nameTvParams.rightMargin = ConvertUtils.dp2px(30);
        nameTv.setLayoutParams(nameTvParams);
        nameTv.setTextColor(Color.WHITE);
        nameTv.setMaxLines(1);
        nameTv.setSingleLine();
        nameTv.setEllipsize(TextUtils.TruncateAt.END);
        nameTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 32);
        nameTv.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        nameTv.setText("张三");
        relativeLayout.addView(nameTv);


        TM.postUIDelay(() -> {
            // 加载重金霸屏四个字
            ImageView bigScreenIv = getIv(180, 50);
            RelativeLayout.LayoutParams bigScreenParams = (RelativeLayout.LayoutParams) bigScreenIv.getLayoutParams();
            bigScreenParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            bigScreenParams.bottomMargin = ConvertUtils.dp2px(65);
            bigScreenIv.setLayoutParams(bigScreenParams);
            layoutRoot.addView(bigScreenIv);
            APNGDrawable apngDrawable1 = getApngDrawable("重金霸屏四个字.png");
            apngDrawable1.setLoopLimit(1);
            GlideHelper.loadApng(apngDrawable1, bigScreenIv);
        }, 600);
    }

    /**
     * 移除SUPERSHOW+美元符号+头像栏 位移动画从右到左消失
     */
    private void doTask6() {
        TranslateAnimation translateAnimation = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0,
                TranslateAnimation.ABSOLUTE, -ScreenUtils.getScreenWidth(),
                TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.ABSOLUTE, 0);
        translateAnimation.setDuration(200);
        translateAnimation.setFillAfter(true);
        for (int i = 0; i < layoutRoot.getChildCount(); i++) {
            final int viewIndex = i;
            translateAnimation.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation animation) {
                }

                @Override
                public void onAnimationEnd(Animation animation) {
                    layoutRoot.removeView(layoutRoot.getChildAt(viewIndex));
                }

                @Override
                public void onAnimationRepeat(Animation animation) {
                }
            });
            layoutRoot.getChildAt(i).startAnimation(translateAnimation);
        }
        TM.postUIDelay(() -> this.doTaskByIndex(7), 200);
    }

    /**
     * 重金霸屏全填充与空心文字左右移动
     */
    private void doTask7() {
        //根布局
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        RelativeLayout.LayoutParams layoutParams = getParams(-1, -1);
        linearLayout.setLayoutParams(layoutParams);
        layoutRoot.addView(linearLayout);

        String[] fileNames = {"重金霸屏full.png", "重金霸屏empty.png", "重金霸屏full.png", "重金霸屏full.png", "重金霸屏empty.png", "重金霸屏full.png"};
        TranslateAnimation animTranslateToLeft = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0,
                TranslateAnimation.ABSOLUTE, -30,
                TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.ABSOLUTE, 0);
        TranslateAnimation animTranslateToRight = new TranslateAnimation(TranslateAnimation.ABSOLUTE, -30,
                TranslateAnimation.ABSOLUTE, 0,
                TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.ABSOLUTE, 0);
        animTranslateToLeft.setDuration(3000);
        animTranslateToRight.setDuration(3000);
        animTranslateToLeft.setFillAfter(true);
        animTranslateToRight.setFillAfter(true);
        int ivWidth = ScreenUtils.getAppScreenWidth() + 30;
        int ivHeight = (int) (1.0 * ScreenUtils.getAppScreenHeight() / 6);
        for (int i = 0; i < fileNames.length; i++) {
            ImageView iv = new ImageView(this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ivWidth, ivHeight);
            iv.setLayoutParams(params);
            iv.setScaleType(ImageView.ScaleType.FIT_XY);
            GlideHelper.loadAssets(fileNames[i], iv);
            linearLayout.addView(iv);
            if (i == 1 || i == 4) {//往右
                iv.startAnimation(animTranslateToRight);
            } else {//往左
                iv.startAnimation(animTranslateToLeft);
            }
        }
        this.doTaskByIndex(8, 400);
    }

    /**
     * 图片+边框从上往下动画+底部撞击火焰
     */
    private void doTask8() {
        //加载图片
        ImageView iv = new ImageView(this);
        RelativeLayout.LayoutParams ivParams = new RelativeLayout.LayoutParams(ConvertUtils.dp2px(415),
                ConvertUtils.dp2px(595));
        ivParams.topMargin = ConvertUtils.dp2px(124);
        ivParams.leftMargin = ConvertUtils.dp2px(30);
        iv.setLayoutParams(ivParams);
        iv.setAdjustViewBounds(true);
        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        GlideHelper.load2("https://c-ssl.duitang.com/uploads/item/201610/30/20161030153526_EvQ8M.thumb.700_0.jpeg", iv);
        layoutRoot.addView(iv);

        //加载图片边框
        ImageView frameIv = getIv();
        frameIv.setScaleType(ImageView.ScaleType.FIT_XY);
        RelativeLayout.LayoutParams frameParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ConvertUtils.dp2px(620));
        frameParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
        frameParams.topMargin = ConvertUtils.dp2px(110);
        frameIv.setLayoutParams(frameParams);
        frameIv.setScaleX(1.15f);
        frameIv.setScaleY(1.15f);
        APNGDrawable apngDrawableFrame = getApngDrawable("照片框_00023_iSpt.png");
        apngDrawableFrame.setLoopLimit(1);
        GlideHelper.loadApng(apngDrawableFrame, frameIv);
        layoutRoot.addView(frameIv);

        //加载撞击火焰
        ImageView fireIv = new ImageView(this);
        RelativeLayout.LayoutParams fireParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        fireParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        fireParams.bottomMargin = -ConvertUtils.dp2px(70);
        fireIv.setLayoutParams(fireParams);
        fireIv.setScaleX(2);
        fireIv.setScaleY(2);
        APNGDrawable apngDrawableFire = getApngDrawable("撞击火焰_00023_iSpt.png");
        apngDrawableFrame.setLoopLimit(1);
        apngDrawableFire.registerAnimationCallback(new Animatable2Compat.AnimationCallback() {
            @Override
            public void onAnimationEnd(Drawable drawable) {
                super.onAnimationEnd(drawable);
                layoutRoot.removeView(fireIv);
            }
        });
        GlideHelper.loadApng(apngDrawableFire, fireIv);
        layoutRoot.addView(fireIv);

        //位移出去动画
        TranslateAnimation translateAnimationOut = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0,
                TranslateAnimation.ABSOLUTE, 0,
                TranslateAnimation.ABSOLUTE, 1,
                TranslateAnimation.ABSOLUTE, ScreenUtils.getAppScreenHeight());
        translateAnimationOut.setFillAfter(true);
        translateAnimationOut.setDuration(200);
        translateAnimationOut.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                layoutRoot.removeAllViews();
                MainActivity.this.doTaskByIndex(9);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });

        //慢速下移动画
        TranslateAnimation translateAnimationSlow = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0,
                TranslateAnimation.ABSOLUTE, 0,
                TranslateAnimation.ABSOLUTE, 1,
                TranslateAnimation.ABSOLUTE, ConvertUtils.dp2px(20));
        translateAnimationSlow.setFillAfter(true);
        translateAnimationSlow.setDuration(3000);
        translateAnimationSlow.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                frameIv.startAnimation(translateAnimationOut);
                iv.startAnimation(translateAnimationOut);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });

        // 加载快速位移动画
        TranslateAnimation translateAnimationFast = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0,
                TranslateAnimation.ABSOLUTE, 0,
                TranslateAnimation.ABSOLUTE, -ScreenUtils.getScreenHeight(),
                TranslateAnimation.ABSOLUTE, 1);
        translateAnimationFast.setFillAfter(true);
        translateAnimationFast.setDuration(400);
        translateAnimationFast.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                frameIv.startAnimation(translateAnimationSlow);
                iv.startAnimation(translateAnimationSlow);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        frameIv.startAnimation(translateAnimationFast);
        iv.startAnimation(translateAnimationFast);
    }

    /**
     * 两个镜像美元+动态文案+静态图
     */
    private void doTask9() {
        //背景位移动画
        TranslateAnimation translateAnimation = new TranslateAnimation(TranslateAnimation.ABSOLUTE, ScreenUtils.getScreenWidth(),
                TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.ABSOLUTE, 0);
        translateAnimation.setDuration(200);
        translateAnimation.setFillAfter(true);
        ImageView bgIv = getIv(-1, -2);
        RelativeLayout.LayoutParams bgParams = (RelativeLayout.LayoutParams) bgIv.getLayoutParams();
        bgParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        bgIv.setAlpha(0.8f);
        bgIv.setScaleX(3.3f);
        bgIv.setScaleY(3.3f);

        GlideHelper.loadAssets("zi_00000", bgIv);
        layoutRoot.addView(bgIv);
        bgIv.startAnimation(translateAnimation);

        //加载美元符号
        for (int i = 0; i < 4; i++) {
            ImageView dollarIv = getIv(i % 2 == 1 ? 270 : 280, -2);
            if (i > 1) {
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) dollarIv.getLayoutParams();
                params.addRule(RelativeLayout.ALIGN_PARENT_END);
                dollarIv.setLayoutParams(params);
                dollarIv.setScaleX(-1);
            }
            APNGDrawable apngDrawable = getApngDrawable("美元链条_00031_iSpt.png");
            apngDrawable.setLoopLimit(1);
            GlideHelper.loadApng(apngDrawable, dollarIv);
            layoutRoot.addView(dollarIv);
        }

        //加载文案+tag根布局
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        RelativeLayout.LayoutParams linearLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        linearLayoutParams.topMargin = ConvertUtils.dp2px(390);
        linearLayoutParams.leftMargin = 20;
        linearLayoutParams.rightMargin = 20;
        linearLayout.setLayoutParams(linearLayoutParams);
        layoutRoot.addView(linearLayout);

        //加载动态文案
        TextView tv = new TextView(this);
        RelativeLayout.LayoutParams tvParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        tv.setLayoutParams(tvParams);
        tv.setTextColor(Color.parseColor("#ffdb88"));
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 58);
        tv.setText("手机不贴膜\n吃面不喝汤\n人不在帅有钱则行");
        tv.setGravity(Gravity.CENTER_HORIZONTAL);
        tv.setLineSpacing(25, 1);
        tv.setShadowLayer(-20f, 0, 0, Color.WHITE);
        linearLayout.addView(tv);

        //加载静态贴图
        ImageView tagIv = getIv(350, -2);
        GlideHelper.loadAssets("tag.png", tagIv);
        tagIv.setVisibility(View.GONE);
        linearLayout.addView(tagIv);
        TranslateAnimation tagTranslateAnim = new TranslateAnimation(TranslateAnimation.ABSOLUTE, ScreenUtils.getScreenWidth(),
                TranslateAnimation.ABSOLUTE, ConvertUtils.dp2px(100),
                TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.ABSOLUTE, 0);
        tagTranslateAnim.setFillAfter(true);
        tagTranslateAnim.setDuration(300);

        //文案加载动画
        ScaleAnimation scaleAnimation = new ScaleAnimation(2.0f, 1.0f, 2.0f, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(500);
        scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                tagIv.setVisibility(View.VISIBLE);
                tagIv.startAnimation(tagTranslateAnim);
                MainActivity.this.doTaskByIndex(10, 1000);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        tv.startAnimation(scaleAnimation);
    }

    /**
     * 侧门打开转场+金币+图片
     */
    private void doTask10() {
        layoutRoot.removeAllViews();
        //加载图片
        ImageView iv = getIv(-1, -1);
        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        GlideHelper.load2("https://c-ssl.duitang.com/uploads/item/201610/30/20161030153526_EvQ8M.thumb.700_0.jpeg", iv);
        layoutRoot.addView(iv);

        //加载金币
        LinearLayout topLinearLayout = new LinearLayout(this);
        RelativeLayout.LayoutParams linearLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        linearLayoutParams.topMargin = ConvertUtils.dp2px(30);
        topLinearLayout.setGravity(Gravity.END);
        topLinearLayout.setLayoutParams(linearLayoutParams);
        LinearLayout bottomLinearLayout = new LinearLayout(this);
        RelativeLayout.LayoutParams bottomLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        bottomLayoutParams.bottomMargin = ConvertUtils.dp2px(30);
        bottomLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        bottomLinearLayout.setLayoutParams(bottomLayoutParams);
        layoutRoot.addView(topLinearLayout);
        layoutRoot.addView(bottomLinearLayout);
        for (int i = 0; i < 6; i++) {
            ImageView coinIv = getIv(100, 100);
            RelativeLayout.LayoutParams coinParams = (RelativeLayout.LayoutParams) coinIv.getLayoutParams();
            coinParams.leftMargin = i < 3 ? ConvertUtils.dp2px(20) : 0;
            coinParams.rightMargin = i < 3 ? 0 : ConvertUtils.dp2px(20);
            coinIv.setLayoutParams(coinParams);
            APNGDrawable apngDrawable = getApngDrawable("金币_loop_iSpt.png");
            apngDrawable.setLoopLimit(1);
            GlideHelper.loadApng(apngDrawable, coinIv);
            if (i < 3) {
                coinIv.setScaleX(-1);
                topLinearLayout.addView(coinIv);
            } else
                bottomLinearLayout.addView(coinIv);
        }

        //加载转场
        ImageView transitionIv = getIv(-1, -1);
        transitionIv.setScaleType(ImageView.ScaleType.FIT_XY);
        APNGDrawable apngDrawable = getApngDrawable("侧门打开_00397_iSpt.png");
        apngDrawable.setLoopLimit(1);
        GlideHelper.loadApng(apngDrawable, transitionIv);
        layoutRoot.addView(transitionIv);

        //再次转场
        TM.postUIDelay(() -> this.doTaskByIndex(11), 3000);
    }

    /**
     * 背景文字+动画文字+金币
     */
    private void doTask11() {
        //背景图
        ImageView bgIv1 = getIv(-1, -2);
        RelativeLayout.LayoutParams bgParams1 = (RelativeLayout.LayoutParams) bgIv1.getLayoutParams();
        bgParams1.topMargin = ConvertUtils.dp2px(80);
        bgIv1.setLayoutParams(bgParams1);
        bgIv1.setAlpha(0.5f);
        GlideHelper.loadAssets("zi_00000.png", bgIv1);
        layoutRoot.addView(bgIv1);

        ImageView bgIv2 = getIv(-1, -2);
        RelativeLayout.LayoutParams bgParams2 = (RelativeLayout.LayoutParams) bgIv2.getLayoutParams();
        bgParams2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        bgParams2.bottomMargin = ConvertUtils.dp2px(90);
        bgIv2.setLayoutParams(bgParams2);
        bgIv2.setAlpha(0.5f);
        GlideHelper.loadAssets("zi_00000.png", bgIv2);
        layoutRoot.addView(bgIv2);

        //加载转场
        ImageView transitionIv = getIv(-1, -1);
        transitionIv.setScaleType(ImageView.ScaleType.FIT_XY);
        APNGDrawable apngDrawable = getApngDrawable("侧门打开_00397_iSpt.png");
        apngDrawable.setLoopLimit(1);
        GlideHelper.loadApng(apngDrawable, transitionIv);
        layoutRoot.addView(transitionIv);
        for (int i = 0; i < layoutRoot.getChildCount() - 6; i++) {
            layoutRoot.removeView(layoutRoot.getChildAt(i));
        }

        //加载动态文案
        TextView tv = new TextView(this);
        RelativeLayout.LayoutParams tvParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        tvParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        tvParams.leftMargin = 20;
        tvParams.rightMargin = 20;
        tv.setLayoutParams(tvParams);
        tv.setTextColor(Color.parseColor("#ffdb88"));
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 58);
        tv.setText("手机不贴膜\n吃面不喝汤\n人不在帅有钱则行");
        tv.setGravity(Gravity.CENTER_HORIZONTAL);
        tv.setLineSpacing(25, 1);
        tv.setShadowLayer(-20f, 0, 0, Color.WHITE);
        layoutRoot.addView(tv);
        //文案加载动画
        ScaleAnimation scaleAnimation = new ScaleAnimation(2.0f, 1.0f, 2.0f, 1.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        scaleAnimation.setDuration(500);
        tv.startAnimation(scaleAnimation);
        this.doTaskByIndex(12, 3000);
    }

    /**
     * 转场+图片+文字动画+标签tag
     */
    private void doTask12() {
        //加载图片
        ImageView iv = getIv(-1, -1);
        iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
        GlideHelper.load2("https://c-ssl.duitang.com/uploads/item/201610/30/20161030153526_EvQ8M.thumb.700_0.jpeg", iv);
        layoutRoot.addView(iv);

        //加载转场
        ImageView transitionIv = getIv(-1, -1);
        transitionIv.setScaleType(ImageView.ScaleType.FIT_XY);
        APNGDrawable apngDrawable = getApngDrawable("侧门打开_00397_iSpt.png");
        apngDrawable.setLoopLimit(1);
        GlideHelper.loadApng(apngDrawable, transitionIv);
        layoutRoot.addView(transitionIv);
        for (int i = 0; i < layoutRoot.getChildCount() - 2; i++) {
            layoutRoot.removeView(layoutRoot.getChildAt(i));
        }

        //加载文案+tag根布局
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        RelativeLayout.LayoutParams linearLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        linearLayoutParams.topMargin = ConvertUtils.dp2px(90);
        linearLayoutParams.leftMargin = ConvertUtils.dp2px(160);
        linearLayoutParams.rightMargin = ConvertUtils.dp2px(10);
        linearLayout.setLayoutParams(linearLayoutParams);
        layoutRoot.addView(linearLayout);

        //加载动态文案
        TextView tv = new TextView(this);
        RelativeLayout.LayoutParams tvParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        tv.setLayoutParams(tvParams);
        tv.setTextColor(Color.parseColor("#ffdb88"));
        tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 38);
        tv.setText("手机不贴膜\n吃面不喝汤\n人不在帅有钱则行");
        tv.setGravity(Gravity.LEFT);
        tv.setLineSpacing(25, 1);
        tv.setShadowLayer(-20f, 0, 0, Color.WHITE);
        tv.setVisibility(View.GONE);
        linearLayout.addView(tv);

        //慢速右移动画
        TranslateAnimation translateAnimationSlow = new TranslateAnimation(TranslateAnimation.ABSOLUTE, ConvertUtils.dp2px(20),
                TranslateAnimation.ABSOLUTE, 0,
                TranslateAnimation.ABSOLUTE, 0,
                TranslateAnimation.ABSOLUTE, 0);
        translateAnimationSlow.setFillAfter(true);
        translateAnimationSlow.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                TM.postUIDelay(() -> {
                    layoutRoot.removeAllViews();
                    MainActivity.this.restart();
                }, 1000);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        translateAnimationSlow.setDuration(3000);

        //加载静态贴图
        ImageView tagIv = getIv(280, -2);
        RelativeLayout.LayoutParams tagParams = (RelativeLayout.LayoutParams) tagIv.getLayoutParams();
        tagParams.topMargin = ConvertUtils.dp2px(100);
        tagIv.setLayoutParams(tagParams);
        GlideHelper.loadAssets("tag.png", tagIv);
        tagIv.setVisibility(View.GONE);
        linearLayout.addView(tagIv);
        TranslateAnimation tagTranslateAnim = new TranslateAnimation(TranslateAnimation.ABSOLUTE, ScreenUtils.getScreenWidth(),
                TranslateAnimation.ABSOLUTE, ConvertUtils.dp2px(20),
                TranslateAnimation.ABSOLUTE, 0, TranslateAnimation.ABSOLUTE, 0);
        tagTranslateAnim.setFillAfter(true);
        tagTranslateAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                tagIv.startAnimation(translateAnimationSlow);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        tagTranslateAnim.setDuration(300);

        //文案加载动画
        AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
        alphaAnimation.setDuration(300);
        alphaAnimation.setFillAfter(true);
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                tagIv.setVisibility(View.VISIBLE);
                tagIv.startAnimation(tagTranslateAnim);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        TM.postUIDelay(() -> {
            tv.setVisibility(View.VISIBLE);
            tv.startAnimation(alphaAnimation);
        }, 1000);
    }
}